/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;


import com.techshop.dao.TechnicalSpecificationsDao;
import com.techshop.model.TechnicalSpecifications;
import java.util.List;
import org.hibernate.Query;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author hai
 */
public class TechnicalSpecificationsDaoImpl extends HibernateDaoSupport implements TechnicalSpecificationsDao{

    @Override
    public List<TechnicalSpecifications> findAll() {
         try
        {
            return getSession().createQuery("from TechnicalSpecifications").list();
        }
        catch (Exception e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
//    @Override
//    public List<TechnicalSpecifications> findAll(int phoneID) {
//         try
//        {
//            List<TechnicalSpecifications> l= getSession().createQuery("from TechnicalSpecifications").list();
//            List<
//        }
//        catch (Exception e)
//        {
//            System.err.printf("Error: ", e.toString());
//            return null;
//        }
//    }

    @Override
    public TechnicalSpecifications getByID(int tsID) {
        // return getSession().createQuery("FROM District WHERE province.id=:provinceId").setInteger("provinceId", provinceId).list();
          try
        {
            return (TechnicalSpecifications) getSession().get(TechnicalSpecifications.class, tsID);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
    @Override
    public List<TechnicalSpecifications> findAllByPhoneID(int phoneID) {
         try
        {
//            //from Cat as cat
//    inner join cat.mate as mate
//    left outer join cat.kittens as kitten
            //from Book book left join fetch book.chapters where book.isbn = ?");
            
//             try
//        {
//            String sql = "select * from color c " +
//                            "where c.id not in (select color_id from phone_code where phone_id = :phoneId)";
//            Query results = getSession().createSQLQuery(sql).addEntity(Color.class);
//            return results.setParameter("phoneId", phoneId).list();
//        }
            String sql = "select * from technical_specifications ts " +
                           "where ts.id not in (select tech_spec_id from phone_technical_specifications where phone_id = :phoneId)";
          Query results = getSession().createSQLQuery(sql).addEntity(TechnicalSpecifications.class);
             return results.setParameter("phoneId", phoneID).list();
        }
        catch (Exception e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
    
}
